package itbour.onetouchshow.activity.version1_2;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.OneToucDialog;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.andview.adapter.BaseRecyclerHolder;
import com.xiao.nicevideoplayer.EditNiceVideoPlayer;
import com.xiao.nicevideoplayer.EditTxVideoPlayerController;
import com.xiao.nicevideoplayer.LogUtil;
import com.xiao.nicevideoplayer.NiceVideoPlayer;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import itbour.onetouchshow.App;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.login.ExtractFrameWorkThread;
import itbour.onetouchshow.activity.login.VideoExtractFrameAsyncUtils;
import itbour.onetouchshow.base.BaseRefreshAdapter;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.bean.ClipVideoBean;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.listener.OnMultiClickListener;
import itbour.onetouchshow.service.HomeKeyWatcher;
import itbour.onetouchshow.singlecase.InchingDataSingleCase;
import itbour.onetouchshow.utils.DateUtils;
import itbour.onetouchshow.utils.FileUtils;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.TypeCastHelper;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.utils.aliyun.AliRequestUtils;
import itbour.onetouchshow.utils.video.EditPlayVideoFrames;

/**
 * Created by Administrator on 2018/1/23.
 */

public class CutVideoActivity extends MVPBaseActivity<CutVideoContract.View, CutVideoPresenter> implements CutVideoContract.View {


    protected final MainHandler mUIHandler = new MainHandler();
    protected boolean isConver = false;
    @BindView(R.id.id_cusvideoplay)
    protected EditNiceVideoPlayer landLayoutVideo;
    @BindView(R.id.ig_seletor)
    protected ImageView igSeletor;
    @BindView(R.id.rv_video)
    protected RecyclerView rvVideo;
    @BindView(R.id.tv_start)
    protected TextView tvStart;
    @BindView(R.id.tv_end)
    protected TextView tvEnd;
    @BindView(R.id.id_ry_count)
    protected RelativeLayout idRyCount;
    @BindView(R.id.id_rv_root)
    protected LinearLayout idRyRoot;
    protected VideoAdapter videoAdapter;
    protected List<Bitmap> list = new ArrayList<>();
    /**
     * 每毫秒所占的px
     */
    protected float averageMsPx;//
    /**
     * 最小剪辑时间
     */
    protected long MIN_CUT_DURATION;
    /**
     * 裁剪基础框的宽度
     */
    protected double mscaleWidth = UIUtils.WH()[0] * 0.56;
    /**
     * 默认itme宽度
     */
    protected int mItemWidth = UIUtils.WHD()[0] / 4;
    /**
     * 计算视频时长所占总宽度
     */
    protected double maxWidth;
    /**
     * 视频总时长
     */
    protected long mDuration;
    /**
     * 抽帧总数
     */
    protected int MAX_COUNT_RANGE;
    /**
     * 左边距
     */
    protected int leftMargen = UIUtils.dp2px(40);
    protected boolean pressedHome;
    protected HomeKeyWatcher mHomeKeyWatcher;
    protected EditTxVideoPlayerController controller;
    protected boolean isPause = false;
    protected boolean isallowPlay = true;
    /**
     * 裁剪时刻的封面图
     */
    protected Bitmap currentBip;
    /**
     * 截取视频的开始时间
     */
    protected long startTime;
    /**
     * 视频宽高以及URL
     */
    protected String videoUrl;
    protected int videoW;
    protected int videoH;
    protected int scrollX;
    /**
     * rv平滑动了多少px
     */
    protected int currentPosTag;
    protected ExtractFrameWorkThread mExtractFrameWorkThread;
    //protected OneTouchProgressDialog oneTouchProgressDialog;
    OneToucDialog oneToucDialog;

    @Override
    protected void initTitle() {
        super.initTitle();
        setTitleText("视频剪辑");
        getToolBarView().getRightTextView().setVisibility(View.VISIBLE);
        getToolBarView().getRightTextView().setText("保存");
        getToolBarView().getRightTextView().setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (list == null || list.size() == 0) {
                    T_.showCustomToast("请稍等...");
                    return;
                }
                cliptAndSave();
            }
        });

        getToolBarView().getLeftimageView().setVisibility(View.VISIBLE);
        getToolBarView().getLeftimageView().setImageResource(R.mipmap.cancle_icon);
        getToolBarView().getLeftimageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


    }

    protected boolean isConverPage() {
        return isConver;
    }

    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_photo_seletor;
    }

    public void showNewDialog() {
        OneToucDialog.Builder builder = new OneToucDialog.Builder(this);
        builder.content("视频上传中");
        oneToucDialog = builder.setType(OneToucDialog.Builder.ONE_TOUCH_TYPE_DOWNLOAD)
                .cancelable(false).setShowDownloadProgress(true).setmWidth(AppConst.APP_DIALOG_WIDTH)
                .show();
    }


    public void cliptAndSave() {
        String newVideoPath = FileUtils.getDownloadDir() + DateUtils.getYMD() + FileUtils.getMP4FileName(videoUrl);
        String Video = ".mp4";
        String IMAGE = ".png";
        String videoName = AliRequestUtils.getInstance().createFileName("video", App.getLoginBean().getUserInfo().getUserId() + "") + Video;
        String imageName = AliRequestUtils.getInstance().createFileName("image", App.getLoginBean().getUserInfo().getUserId() + "") + IMAGE;
        //oneTouchProgressDialog = new OneTouchProgressDialog();
        //oneTouchProgressDialog.setTitle("视频上传中");
        showNewDialog();
        showProgressDialogWithText("视频剪辑中...");
        //mPresenter.cliptAndSave();
        if (EditPlayVideoFrames.startclipDecodeVideo(videoUrl, newVideoPath, startTime, MIN_CUT_DURATION)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        AliRequestUtils.getInstance().setName(imageName);
                        if (currentBip == null) {
                            currentBip = list.get(1);
                        }
                        File imagefile = AliRequestUtils.getInstance().saveBitmapFile(currentBip, FileUtils.getJpgFileName(videoUrl));
                        App.ossClient.putObject(AliRequestUtils.getInstance().putObjectRequest(imagefile.getAbsolutePath()));
                        //App.ossClient.putObject(list.get(0));
                        String imagePathName = AppConfig.UPLOAD_ALYUN + imageName;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dismissProgressDialog();
                                //oneTouchProgressDialog.builderApk(CutVideoActivity.this);
                            }
                        });
                        L_.e("newVideoPath" + newVideoPath);
                        AliRequestUtils.getInstance().setName(videoName);
                        PutObjectRequest put = new PutObjectRequest(AppConst.OSSBUCKETNAME, videoName, newVideoPath);
                        // 异步上传时可以设置进度回调
                        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
                            @Override
                            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                                L_.e("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
                                int progress = (int) (currentSize * 100 / totalSize);
                                L_.e(progress + "% ");
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        //oneTouchProgressDialog.getTvprogress().setText(progress + "% ");
                                        // oneTouchProgressDialog.getProgressBar().setProgress(progress);
                                        //oneToucDialog.
                                        oneToucDialog.setTvprogress(progress + "% ");
                                        oneToucDialog.setDownloadProgress(progress);

                                    }
                                });
                                if (progress == 100) {
                                    String videoPathName = AppConfig.UPLOAD_ALYUN + videoName;
                                    Intent intent = new Intent();
                                    String[] strings = UIUtils.videoWHDA1(newVideoPath);
                                    ClipVideoBean videoBean = new ClipVideoBean();
                                    videoBean.setW(strings[0]);
                                    videoBean.setH(strings[1]);
                                    videoBean.setD(strings[2]);
                                    videoBean.setR(strings[3]);
                                    videoBean.setVideoUrl(videoPathName);
                                    videoBean.setVideoThumb(imagePathName);
                                    intent.putExtra(AppConst.VIDEOBEAN, videoBean);
                                    setResult(8000, intent);
                                    oneToucDialog.dismiss();
                                    releaseAll();
                                    finish();
                                }
                            }
                        });

                        App.ossClient.putObject(put);
                    } catch (Exception e) {
                        e.printStackTrace();
                        T_.showToastWhendebug("上传失败");
                       /* if (oneTouchProgressDialog != null && oneTouchProgressDialog.isVisible()) {
                            oneTouchProgressDialog.dismiss();
                        }*/
                        if (oneToucDialog != null) {
                            oneToucDialog.dismiss();
                        }
                        dismissProgressDialog();
                    }
                }
            }).start();


        } else {
            T_.showCustomToast("保存失败，请稍后再试");
            dismissProgressDialog();
        }
    }


    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
        //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//显示状态栏
        //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN );
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            videoUrl = extras.getString(AppConst.VIDEOURL);
            videoW = Integer.valueOf(extras.getString(AppConst.VIDEOW));
            videoH = Integer.valueOf(extras.getString(AppConst.VIDEOH));
        }

        baseInit();
        reSizeOfVideoParams();
        onStartPlay(videoUrl, "");
        calculateViewLayout();
        initRecycleView();
        showSelectCutTimeView();
        onstartGetVideImage();
    }

    protected void initRecycleView() {
        rvVideo.setHasFixedSize(true);
        LinearLayoutManager ms = new LinearLayoutManager(CutVideoActivity.this);
        ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布局方式为横向布局
        //  LinearLayoutManager 种 含有3 种布局样式  第一个就是最常用的 1.横向 , 2. 竖向,3.偏移
        rvVideo.setLayoutManager(ms);
        rvVideo.addOnScrollListener(setOnRecyclviewSollerListener());
        list.clear();
        for (int i = 0; i < MAX_COUNT_RANGE + 2; i++) {
            list.add(null);
        }
        videoAdapter = new VideoAdapter(CutVideoActivity.this, list, R.layout.video_item);
        rvVideo.setAdapter(videoAdapter);
    }

    /**
     * 设置布局位置  以及计算RecycleviewItem的宽度
     */
    protected void calculateViewLayout() {
        MIN_CUT_DURATION = setMinDuration();
        idRyRoot.setVisibility(View.GONE);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) igSeletor.getLayoutParams();
        igSeletor.setScaleType(ImageView.ScaleType.FIT_XY);
        //避免抽帧超出
        layoutParams.width = (int) mscaleWidth + UIUtils.dp2px(2);
        layoutParams.leftMargin = leftMargen - UIUtils.dp2px(1);

        RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) idRyCount.getLayoutParams();
        layoutParams2.width = (int) mscaleWidth;
        layoutParams2.leftMargin = leftMargen;

        String s = UIUtils.videoWHDA(videoUrl)[2];
        int duration = (int) (Double.valueOf(s) * 1000);
        // initEditVideo(Long.valueOf(String.valueOf(duration)));
        mDuration = duration;
        //分成是N等分
        MAX_COUNT_RANGE = Integer.valueOf(String.valueOf(mDuration / 1000));
        if (mDuration < MIN_CUT_DURATION) {
            MIN_CUT_DURATION = mDuration;
        }
        //取整
        DecimalFormat df = new DecimalFormat("#.#");
        double end = MIN_CUT_DURATION / 1000;
        String endTimeTv = df.format(end);
        tvEnd.setText(endTimeTv + "s");
        float scale = MIN_CUT_DURATION * 1.0f / duration;
        //减去左边的间距
        maxWidth = mscaleWidth / scale;
        averageMsPx = (float) (duration / maxWidth);
        //重新设置ItmeWidth
        mItemWidth = (int) Math.ceil(maxWidth / MAX_COUNT_RANGE);
    }


    protected void showSelectCutTimeView() {
        if (idRyRoot != null && idRyRoot.getVisibility() == View.GONE) {
            idRyRoot.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 设置video宽高
     */
    protected void reSizeOfVideoParams() {
        RelativeLayout.LayoutParams videoLayoutParams = (RelativeLayout.LayoutParams) landLayoutVideo.getLayoutParams();
        if (Integer.valueOf(videoH) > Integer.valueOf(videoW)) {
            int height = (int) (UIUtils.WHD()[1] * 0.52);
            float scale = videoH * 1.0f / height;
            videoLayoutParams.width = (int) (videoW / scale);
            videoLayoutParams.height = height;

        } else if (Integer.valueOf(videoH) == Integer.valueOf(videoW)) {
            videoLayoutParams.width = UIUtils.WHD()[0];
            videoLayoutParams.height = UIUtils.WHD()[0];
        } else {
            int width = UIUtils.WHD()[0];
            videoLayoutParams.width = width;
            float scale = videoW * 1.0f / width;
            videoLayoutParams.height = (int) (videoH / scale);
        }
        landLayoutVideo.setLayoutParams(videoLayoutParams);
    }

    protected RecyclerView.OnScrollListener setOnRecyclviewSollerListener() {
        OnRecyclviewSollerListener onRecyclviewSollerListener = new OnRecyclviewSollerListener();
        return onRecyclviewSollerListener;
    }

    public long setMinDuration() {
        return InchingDataSingleCase.INSTANCE.getDuration();
    }

    protected void onstartGetVideImage() {
        mExtractFrameWorkThread = new ExtractFrameWorkThread(2, mItemWidth, UIUtils.dip2px(80),
                mUIHandler, videoUrl, null, 0, mDuration, MAX_COUNT_RANGE);
        mExtractFrameWorkThread.start();

    }


    @Override
    public void setVideouserdoccoverSuccess() {

    }


    @Override
    public void loadDocPreviewSuccess() {

    }

    @Override
    public void getuserDockeyframetimeSuccess() {

    }

    @Override
    public void getKeyFrameTimes(Double[] times) {

    }

    public void releaseAll() {
        if (mExtractFrameWorkThread != null) {
            mExtractFrameWorkThread.stopExtract();
        }
        mUIHandler.removeCallbacksAndMessages(null);
        VideoExtractFrameAsyncUtils.stop = true;
        EditPlayVideoFrames.isStop = true;
    }

    //EditTxVideoPlayerController controller;
    public void baseInit() {
        landLayoutVideo.setPlayerType(NiceVideoPlayer.TYPE_NATIVE); // IjkPlayer or MediaPlayer
        controller = new EditTxVideoPlayerController(getApplicationContext());
        // controller.setImage(UIUtils.getR.drawable.place_icon);
        controller.setNeedPlay(isNeedPlay());
        landLayoutVideo.setController(controller);
        mHomeKeyWatcher = new HomeKeyWatcher(this);
        mHomeKeyWatcher.setOnHomePressedListener(new HomeKeyWatcher.OnHomePressedListener() {
            @Override
            public void onHomePressed() {
                pressedHome = true;
            }
        });
        pressedHome = false;
        mHomeKeyWatcher.startWatch();
    }

    protected void onStartPlay(String viudeUrl, String videoName) {
        if (!TextUtils.isEmpty(viudeUrl)) {
            landLayoutVideo.setUp(viudeUrl, null);
        }
        if (!TextUtils.isEmpty(videoName)) {
            controller.setTitle(videoName);
        }

    }

    protected boolean isNeedPlay() {
        return true;
    }

    protected void onChageUrlStartPlay(String viudeUrl, long startTime, long endTime) {
        if (!TextUtils.isEmpty(viudeUrl)) {
            landLayoutVideo.setmDuration(MIN_CUT_DURATION);
            landLayoutVideo.setchageUrlUp(viudeUrl, startTime, endTime, MIN_CUT_DURATION);
        }
    }

    protected View getFootView() {
        View footview = UIUtils.inflate(R.layout.video_item_place);
        TextView textView = footview.findViewById(R.id.id_ry_place);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) textView.getLayoutParams();
        layoutParams.width = (int) (UIUtils.WH()[0] * 0.44) - leftMargen;
        // layoutParams.height = (int) (UIUtils.WH()[0] / 0.44);
        textView.setLayoutParams(layoutParams);
        LogUtil.i(" layoutParams1.width" + layoutParams.width);
        return footview;
    }

    protected View getHeadView() {
        View footview = UIUtils.inflate(R.layout.video_item_place);
        TextView textView = footview.findViewById(R.id.id_ry_place);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) textView.getLayoutParams();
        layoutParams.width = leftMargen;
        // layoutParams.height = (int) (UIUtils.WH()[0] / 0.44);
        textView.setLayoutParams(layoutParams);
        LogUtil.i(" layoutParams1.width" + layoutParams.width);
        return footview;
    }

    protected int getScrollXDistance() {
        LinearLayoutManager layoutManager = (LinearLayoutManager) rvVideo.getLayoutManager();
        int position = layoutManager.findFirstVisibleItemPosition();
        if (list != null && list.size() > 0) {
            currentPosTag = position;
            currentBip = list.get(position);
        }
        View firstVisibleChildView = layoutManager.findViewByPosition(position);
        int itemWidth = firstVisibleChildView.getWidth();
        return (position) * itemWidth - firstVisibleChildView.getLeft();
    }

    @Override
    protected void onPause() {
        if (isConverPage()) {
            super.onPause();
        } else {
            landLayoutVideo.pause();
            super.onPause();
            isPause = true;
        }

    }

    @Override
    protected void onStop() {
        // 在OnStop中是release还是suspend播放器，需要看是不是因为按了Home键
        if (pressedHome) {
            //getmNiceVideoPlayer().pause();
        }
        super.onStop();
        mHomeKeyWatcher.stopWatch();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isConverPage()) {
            if (landLayoutVideo.isPaused() && isallowPlay) {
                landLayoutVideo.restart();
            } else {
                landLayoutVideo.start();
            }
            isPause = false;
        }

    }

    @Override
    protected void onRestart() {
        mHomeKeyWatcher.startWatch();
        pressedHome = false;
        super.onRestart();
    }

    @Override
    public void onBackPressed() {
        if (onBackPressd()) {
            return;
        }
        releaseAll();
        super.onBackPressed();
    }

    public boolean onBackPressd() {
        if (landLayoutVideo != null) {
            if (landLayoutVideo.isFullScreen()) {
                return landLayoutVideo.exitFullScreen();
            } else if (landLayoutVideo.isTinyWindow()) {
                return landLayoutVideo.exitTinyWindow();
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        if (landLayoutVideo != null) {
            landLayoutVideo.release();
        }
        super.onDestroy();

        releaseAll();
        if (list != null) {
            for (Bitmap b : list) {
                if (b != null && !b.isRecycled()) {
                    b.recycle();
                }
            }
        }
        System.gc();
    }

    @Override
    public void finish() {
        super.finish();
        //注释掉activity本身的过渡动画
        overridePendingTransition(0, 0);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 50, sticky = true)
    public void paySuccess(Integer code) {

    }

    public class OnRecyclviewSollerListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (MIN_CUT_DURATION >= mDuration) {
                return;
            }
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                if (landLayoutVideo != null)
                    landLayoutVideo.restart();
            } else {
                if (landLayoutVideo != null)
                    landLayoutVideo.pause();
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (MIN_CUT_DURATION >= mDuration) {
                return;
            }
            scrollX = getScrollXDistance();
            double startscrollPos = averageMsPx * scrollX;
            landLayoutVideo.seekTo((long) startscrollPos);
            double endscrollPos = averageMsPx * (scrollX + mscaleWidth);
            startTime = TypeCastHelper.toLong(startscrollPos);
            long endTime = TypeCastHelper.toLong(endscrollPos);

            DecimalFormat df = new DecimalFormat("#.#");
            double end = endscrollPos / 1000;
            String endTimeTv = df.format(end);
            double start = startscrollPos / 1000;
            String startTimeTv = df.format(start);

            if (endTime > mDuration) {
                startTime = mDuration - MIN_CUT_DURATION;
                tvStart.setText(startTime / 100 * 1.0 / 10 + "s");
                tvEnd.setText(mDuration / 100 * 1.0 / 10 + "s");
            } else {
                tvStart.setText(startTimeTv + "s");
                tvEnd.setText(endTimeTv + "s");
            }
            onChageUrlStartPlay(videoUrl, startTime, endTime);
        }
    }

    private class MainHandler extends Handler {
        int count = 1;

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == ExtractFrameWorkThread.MSG_SAVE_SUCCESS) {
                if (count >= list.size() - 1) {
                    releaseAll();
                    return;
                }
                Bitmap info = (Bitmap) msg.obj;
                if (currentPosTag == count) {
                    currentBip = info;
                }
                list.set(count, info);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        videoAdapter.setListData(list);
                    }
                });
                count++;

            }
        }
    }

    public class VideoAdapter extends BaseRefreshAdapter<Bitmap> {

        //设置三种不同Item类型,分别是头部,item,尾部
        public static final int ITME_TYPE_HEADER = 1;
        public static final int ITME_TYPE_CONTENT = 2;
        public static final int ITME_TYPE_BOTTOM = 3;

        public VideoAdapter(Context context, List<Bitmap> datas, int itemLayoutId) {
            super(context, datas, itemLayoutId);
        }

        @Override
        protected void convert(BaseRecyclerHolder var1, Bitmap var2, int var3) {
            if (getItemViewType(var3) == ITME_TYPE_CONTENT) {
                ImageView imageView = var1.getView(R.id.id_ig_place);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                layoutParams.width = mItemWidth;
                if (var2 == null) {
                    Drawable drawable = UIUtils.getDrawable(R.drawable.place_icon);//获取drawable
                    BitmapDrawable bd = (BitmapDrawable) drawable;
                    Bitmap bm = bd.getBitmap();
                    imageView.setImageBitmap(bm);
                } else {
                    imageView.setImageBitmap(list.get(var3));
                }
            }
        }


        @Override
        public BaseRecyclerHolder onCreateViewHolder(ViewGroup viewGroup, int i, boolean b) {
            View view;
            if (ITME_TYPE_HEADER == i) {
                view = getHeadView();
            } else if (ITME_TYPE_BOTTOM == i) {
                view = getFootView();
            } else {
                view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.video_item, viewGroup, false);

            }
            return new BaseRecyclerHolder(view);
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0) {
                return ITME_TYPE_HEADER;
            } else if (position == list.size() - 1) {
                return ITME_TYPE_BOTTOM;
            } else {
                return ITME_TYPE_CONTENT;
            }
        }
    }

}
